package com.ljfiction.book.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.ljfiction.book.Baner.DengjiBean;
import com.ljfiction.book.MyView.DividerItemDecoration;
import com.ljfiction.book.R;
import com.ljfiction.book.http.HttpManager;
import com.ljfiction.book.readview.Base;
import com.ljfiction.book.readview.Constant;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Android on 2018/2/1.
 */

public class DengjizhiduFragment extends BaseFragment {


    @BindView(R.id.recycle_dengji)
    RecyclerView recycleDengji;
    @BindView(R.id.refresh_dengji)
    SmartRefreshLayout refreshDengji;
    Unbinder unbinder;
    private List<DengjiBean.DataBean> list = new ArrayList<>();
    private BaseQuickAdapter mAdapter;
    private DengjiBean dengjibean;
    private Context context;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dengjizhidu_fragment, null);
        unbinder = ButterKnife.bind(this, view);
        context=getContext();
        refreshDengji.setEnableLoadMore(false);
        intView();
        intData();
        return view;
    }

    private void intData() {
        OkHttpUtils.get(HttpManager.IP+"/getUserLevenInfo")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        refreshDengji.finishRefresh();
                        Gson gs=new Gson();
                        try {
                            dengjibean = gs.fromJson(s,DengjiBean.class);
                            if (dengjibean.getReturnCode()==200){
                                list.clear();
                                list.addAll(dengjibean.getData());
                                mAdapter.notifyDataSetChanged();
                            }else {
                                showToast(dengjibean.getMsg());
                            }
                        } catch (JsonSyntaxException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    private void intView() {
        recycleDengji.setAdapter(mAdapter= new BaseQuickAdapter<DengjiBean.DataBean, BaseViewHolder>(R.layout.dengjizhidu_item,list) {
            @Override
            protected void convert(BaseViewHolder helper, DengjiBean.DataBean item) {
                AutoUtils.autoSize(helper.itemView);
                helper.setText(R.id.dengjihao,String.valueOf(item.getFansLevel())).setText(R.id.dengjijifen,String.valueOf(item.getFansIntegral()));
            }
        });
        recycleDengji.setLayoutManager(new LinearLayoutManager(context));
        recycleDengji.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL_LIST));
        refreshDengji.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                intData();
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
